<?php

namespace app\common\model\xiluedu;

use think\Loader;
use think\Model;
use InvalidArgumentException;
/**
 * 配置模型
 */
class Config extends Model
{

    // 表名,不含前缀
    protected $name = 'xiluedu_config';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;
    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    // 追加属性
    protected $append = [

    ];
    public $imageField = ['avatar', 'icon', 'image', 'custom_qrcode', 'logo'];
    public $imagesField = ['images'];

    /**
     * 资源路径补全, /开头的为上传资源，http开头的不处理,其它为本地资源
     * @param        $url
     * @param  bool  $version
     *
     * @return string
     */
    function assets($url, $version = false) {
        if (stripos($url, 'http') === 0) {

        } else {
            if (stripos($url, '/') === 0) {
                $url = cdnurl($url, true);
            }  else {
                $url = config('site.cdnurl') . '/' . $url;
            }
        }
        if ($version) {
            if (stripos($url, '?') === false) {
                $url .= '?v=' .config('site.version');
            } else {
                $url .= '&v=' .config('site.version');
            }
        }
        return  $url;
    }


    /**
     * 获取对应的name配置
     * @param null $name
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public static function getMyConfig($name=NULL){
        $config = [];
        $configList = self::whereNotIn('name',['distribution','apply','minimessage'])->select();
        foreach ($configList as $k => $v) {
            $value = $v->toArray();
            if ($value['type'] == 'array') {
                $value['value'] = (array)json_decode($value['value'], true);
            }
            $config[$value['name']] = $value['value'];
        }
        if($name){
            return isset($config[$name]) ? $config[$name] : [];
        }
        return $config;
    }

}
